//
//  AYSWatchRecordViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/2.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSWatchRecordViewController.h"
#import "FMDatabase.h"

@interface AYSWatchRecordViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *watchRecordArray;

@property (nonatomic, strong) FMDatabase *dataBase;

@end

@implementation AYSWatchRecordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.text = @"观看记录";
    _watchRecordArray = [NSMutableArray arrayWithCapacity:0];
    
    [self useSQLite];
    [self createTableView];
}

- (void)createTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) style:(UITableViewStylePlain)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"WatchRecord"];
    if (indexPath.row == [_watchRecordArray count])
    {
        cell.textLabel.text = @"清空历史纪录";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    else
    {
        cell.textLabel.text = _watchRecordArray[indexPath.row];
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_watchRecordArray count] + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [_watchRecordArray count])
    {
        [self cencleSQLite];
    }
}

- (void)cencleSQLite
{
    [_watchRecordArray removeAllObjects];
    [_dataBase executeUpdate:@"DELETE FROM WATCHRECORD"];
    [_mainTableView reloadData];
}

- (void)useSQLite
{
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"AYSWatchRecord.db"];
    NSLog(@"%@", filePath);
    _dataBase = [FMDatabase databaseWithPath:filePath];
    if ([_dataBase open])
    {
        [_dataBase executeUpdate:@"CREATE TABLE WATCHRECORD (title text)"];
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"SELECT * FROM WATCHRECORD"];
    while ([resultSet next])
    {
        NSString *title = [resultSet stringForColumn:@"title"];
        [_watchRecordArray addObject:title];
    }
    for (int i = 0; i < [_watchRecordArray count]; ++i)
    {
        for (int j = (int)_watchRecordArray.count - 1; j > i; j--)
        {
            if ([_watchRecordArray[i] isEqualToString:_watchRecordArray[j]])
            {
                [_watchRecordArray removeObjectAtIndex:j];
            }
        }
    }
    
    [resultSet close];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
